import request from '@/Tools/requestApi'
import type {
  ILocation,
  IWeather,
  IHFLocation,
  ILocationData,
  IAirQuality,
  WeatherIndices,
  IWeatherFuture,
  IHourlyWeather,
  IWarning,
} from '@/types/weather'
import { weatherIncludesType } from '@/types/weather'

export class WeatherService {
  /**
   * 获取用户当前位置信息
   */
  static async getCurrentLocation(): Promise<ILocation> {
    const response = await request.Get_Tx_Map_Location()
    return response.data.result
  }

  /**
   * 搜索位置信息
   */
  static async searchLocation(locationName: string): Promise<IHFLocation[]> {
    const response = await request.get_HF_Location(locationName)
    return response.data.location || []
  }

  /**
   * 获取实时天气信息
   */
  static async getWeather(locationId: string): Promise<IWeather> {
    const response = await request.Real_time_weather(locationId)
    return response.data
  }

  /**
   * 转换和风天气位置数据为标准格式
   */
  static convertHFLocationToLocationData(hfLocation: IHFLocation): ILocationData {
    return {
      id: hfLocation.id,
      name: hfLocation.name,
      country: hfLocation.country,
      adm1: hfLocation.adm1,
      adm2: hfLocation.adm2,
      lat: parseFloat(hfLocation.lat),
      lon: parseFloat(hfLocation.lon),
      weather: null,
      isDefault: false,
      isCurrent: false,
      airQuality: null,
      indices: null,
      future: null,
      HourlyWeather: null,
      warningData: null,
    }
  }

  /**
   * 转换腾讯地图位置数据为标准格式
   */
  static convertTxLocationToLocationData(txLocation: ILocation): ILocationData {
    return {
      id: txLocation.ad_info.adcode.toString(),
      name: txLocation.ad_info.district,
      country: txLocation.ad_info.nation,
      adm1: txLocation.ad_info.province,
      adm2: txLocation.ad_info.city,
      lat: txLocation.location.lat,
      lon: txLocation.location.lng,
      weather: null,
      isDefault: false,
      isCurrent: false,
      airQuality: null,
      indices: null,
      future: null,
      HourlyWeather: null,
      warningData: null,
    }
  }

  /**
   * 获取位置完整信息（包括天气）
   */
  static async getLocationWithWeather(locationName: string): Promise<ILocationData> {
    // 搜索位置
    const locations = await this.searchLocation(locationName)
    if (locations.length === 0) {
      throw new Error(`未找到位置: ${locationName}`)
    }

    const locationData = this.convertHFLocationToLocationData(locations[0])

    // 获取天气信息
    const weather = await this.getWeather(locationData.id)
    locationData.weather = weather

    // 获取空气质量
    locationData.airQuality = await this.fetchAirQuality(locationData?.lat, locationData?.lon)
    // 获取天气指数
    locationData.indices = await this.getweatherIndices(1, locationData?.id)
    // 获取未来7天
    locationData.future = await this.getFutureWeather(7, locationData?.id)
    // 获取小时天气
    locationData.HourlyWeather = await this.getHourlyWeather(168, locationData?.id)
    // 获取预警信息
    locationData.warningData = await this.getWarningInfo(locationData?.id)
    return locationData
  }

  /**
   * 初始化默认位置和天气
   */
  static async initializeDefaultLocation(): Promise<ILocationData> {
    // 获取当前位置
    const currentLocation = await this.getCurrentLocation()
    const locationData = this.convertTxLocationToLocationData(currentLocation)

    // 搜索对应的和风天气位置
    const hfLocations = await this.searchLocation(locationData.name)
    if (hfLocations.length > 0) {
      const hfLocation = hfLocations[0]
      locationData.id = hfLocation.id
      locationData.name = hfLocation.name
      locationData.country = hfLocation.country
      locationData.adm1 = hfLocation.adm1
      locationData.adm2 = hfLocation.adm2
      locationData.lat = parseFloat(hfLocation.lat)
      locationData.lon = parseFloat(hfLocation.lon)
    }

    // 获取天气信息
    const weather = await this.getWeather(locationData.id)
    locationData.weather = weather
    locationData.isDefault = true
    locationData.isCurrent = true
    // 获取空气质量
    locationData.airQuality = await this.fetchAirQuality(locationData?.lat, locationData?.lon)
    // 获取天气指数
    locationData.indices = await this.getweatherIndices(1, locationData?.id)
    // 查询未来7天天气
    locationData.future = await this.getFutureWeather(7, locationData?.id)
    // 获取小时天气
    locationData.HourlyWeather = await this.getHourlyWeather(168, locationData?.id)
    // 获取预警信息
    locationData.warningData = await this.getWarningInfo(locationData?.id)
    return locationData
  }
  /**
   * 获取空气质量
   */
  static async fetchAirQuality(
    latitude: string | number,
    longitude: string | number,
  ): Promise<IAirQuality> {
    const response = await request.Get_Air_Quality(latitude, longitude)
    return response.data
  }
  /**
   * 获取天气指数
   */
  static async getweatherIndices(
    days: number,
    location: string,
    type: weatherIncludesType = weatherIncludesType.ALL,
  ): Promise<WeatherIndices> {
    const res = await request.getweatherInclueds(days, type, location)
    return res.data
  }
  /**
   *  获取未来天气
   * @param day 天数
   * @param location 位置
   */
  static async getFutureWeather(day: number = 7, location: string): Promise<IWeatherFuture> {
    const res = await request.getWetherFuture(day, location)
    return res.data
  }
  /**
   * 获取小时天气
   * @param hours 小时
   * @param location 位置
   */
  static async getHourlyWeather(hours: number = 168, location: string): Promise<IHourlyWeather> {
    const res = await request.gethoursWeather(hours, location)
    return res.data
  }
  static async getWarningInfo(location: string): Promise<IWarning> {
    const res = await request.getWarningInfo(location)
    return res.data
  }
}
